home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / watcom / w_modex / xpal.cpp < prev    next >
C/C++ Source or Header  |  1994-02-01  |  5KB  |  266 lines

  1. #include <conio.h>
  2. #include <dos.h>
  3. #include <mem.h>
  4.  
  5. #include "modex.hpp"
  6. #include "xpal.hpp"
  7.  
  8. BYTE Xpal[768];
  9.  
  10. void
  11. set_paletteX(BYTE *pal, FLAG downgrade)
  12. {
  13.     short int i;
  14.     BYTE *buf;
  15.  
  16.     memcpy(Xpal, pal, 768);
  17.  
  18.     buf = Xpal;
  19.     if (downgrade) {
  20.         i=768;
  21.         while (i--) {
  22.             *buf++ = (*buf >> 2);
  23.         }
  24.     }
  25.  
  26.     outp(0x03c8, 0);  // Start with color 0
  27.     buf = Xpal;
  28.     i=256;
  29.     while (i--) {
  30.         outp(0x03c9, *buf++);
  31.         outp(0x03c9, *buf++);
  32.         outp(0x03c9, *buf++);
  33.     }
  34. }
  35.  
  36.  
  37. void
  38. get_paletteX(BYTE *pal, FLAG upgrade)
  39. {
  40.     int i;
  41.  
  42.     memcpy(pal, Xpal, 768);
  43.  
  44.     if (upgrade) {
  45.         i=768;
  46.         while (i--) {
  47.             *pal++ = (*pal << 2);
  48.         }
  49.     }
  50. }
  51.  
  52.  
  53. void
  54. get_BIOSpaletteX(BYTE *pal, FLAG upgrade)
  55. {
  56.     int i;
  57.     union REGS r;
  58.  
  59.     r.x.eax = 0x1017;
  60.     r.x.ebx = 0;
  61.     r.x.ecx = 256;
  62.     r.x.edx = (unsigned long) pal;
  63.  
  64.     int386(0x10, &r, &r);
  65.  
  66.     if (upgrade) {
  67.         i=768;
  68.         while (i--) {
  69.             *pal++ = (*pal << 2);
  70.         }
  71.     }
  72. }
  73.  
  74.  
  75. void
  76. photo_negativeX(void)
  77. {
  78.     short int i;
  79.     BYTE temp_pal[768];
  80.     BYTE *temp;
  81.  
  82.     get_paletteX(temp_pal, 0);
  83.     temp = temp_pal;
  84.  
  85.     for (i=0; i < 256; i++) {
  86.         *temp++ = (64 - (*temp));
  87.         *temp++ = (64 - (*temp));
  88.         *temp++ = (64 - (*temp));
  89.     }
  90.  
  91.     set_paletteX(temp_pal, 0);
  92. }
  93.  
  94.  
  95. void
  96. grey_paletteX(void)
  97. {
  98.     smooth64_paletteX(1, 1, 1);
  99. }
  100.  
  101.  
  102. void
  103. RGB_paletteX(void)
  104. {
  105.     BYTE r, g, b;
  106.     BYTE temp_pal[768];
  107.     BYTE *temp;
  108.  
  109.     temp = temp_pal;
  110.  
  111.     for (r=0; r < 8; r++) {
  112.         for (g=0; g < 8; g++) {
  113.             for (b=0; b < 4; b++) {
  114.                 *temp++ = (r << 3);
  115.                 *temp++ = (g << 3);
  116.                 *temp++ = (b << 4);
  117.             }
  118.         }
  119.     }
  120.  
  121.     set_paletteX(temp_pal, 0);
  122. }
  123.  
  124.  
  125. void
  126. smooth64_paletteX(BYTE r, BYTE g, BYTE b)
  127. {
  128.     short int i;
  129.     BYTE temp_pal[768];
  130.     BYTE *temp;
  131.  
  132.     memset(temp_pal, 0, 768);
  133.  
  134.  
  135.     if (r) {
  136.         temp = temp_pal;
  137.         for (i=0; i < 64; i++) {
  138.             *temp = i;
  139.             temp += 3;
  140.         }
  141.     }
  142.  
  143.     if (g) {
  144.         temp = temp_pal + 1;
  145.         for (i=0; i < 64; i++) {
  146.             *temp = i;
  147.             temp += 3;
  148.         }
  149.     }
  150.  
  151.     if (b) {
  152.         temp = temp_pal + 2;
  153.         for (i=0; i < 64; i++) {
  154.             *temp = i;
  155.             temp += 3;
  156.         }
  157.     }
  158.  
  159.     set_paletteX(temp_pal, 0);
  160. }
  161.  
  162.  
  163. void
  164. brighten_paletteX(SBYTE r, SBYTE g, SBYTE b)
  165. {
  166.     short int i, j, scratch;
  167.     BYTE temp_pal[768];
  168.     BYTE *temp;
  169.     SBYTE dummy[3];
  170.  
  171.     get_paletteX(temp_pal, 0);
  172.     temp = temp_pal;
  173.  
  174.     dummy[0] = r;
  175.     dummy[1] = g;
  176.     dummy[2] = b;
  177.  
  178.     for (i=0; i < 256; i++) {
  179.         for (j=0; j < 3; j++) {
  180.             scratch = *temp + dummy[j];
  181.             if (scratch <= 0) {
  182.                 *temp++ = 0;
  183.             } else if (scratch >= 63) {
  184.                 *temp++ = 63;
  185.             } else {
  186.                 *temp++ = scratch;
  187.             }
  188.         }
  189.     }
  190.  
  191.     set_paletteX(temp_pal, 0);
  192. }
  193.  
  194.  
  195. void
  196. stretch_paletteX(BYTE r, BYTE g, BYTE b)
  197. {
  198.     short int i, j, scratch;
  199.     BYTE temp_pal[768];
  200.     BYTE *temp;
  201.     BYTE dummy[3];
  202.  
  203.     get_paletteX(temp_pal, 0);
  204.     temp = temp_pal;
  205.  
  206.     dummy[0] = r;
  207.     dummy[1] = g;
  208.     dummy[2] = b;
  209.  
  210.     for (i=0; i < 256; i++) {
  211.         for (j=0; j < 3; j++) {
  212.             scratch = ((((*temp - 32) * dummy[j]) + 8) >> 4) + 32;
  213.             if (scratch <= 0) {
  214.                 *temp++ = 0;
  215.             } else if (scratch >= 63) {
  216.                 *temp++ = 63;
  217.             } else {
  218.                 *temp++ = scratch;
  219.             }
  220.         }
  221.     }
  222.  
  223.     set_paletteX(temp_pal, 0);
  224. }
  225.  
  226.  
  227. void
  228. rot_palette(BYTE dist)
  229. {
  230.     int shift, i;
  231.     BYTE temp_pal[768];
  232.  
  233.     shift = (dist * 3);
  234.     memcpy(temp_pal, Xpal + shift, 768 - shift);
  235.     memcpy(temp_pal + (768 - shift), Xpal, shift);
  236.  
  237.     set_paletteX(temp_pal, 0);
  238. }
  239.  
  240.  
  241. BYTE
  242. find_RGB(BYTE r, BYTE g, BYTE b)
  243. {
  244.     long shortest_dist, temp_dist;
  245.     short int i, shortest_pal;
  246.  
  247.     shortest_pal = 0;
  248.     shortest_dist = (r - Xpal[0]) * (r - Xpal[0]) +
  249.                     (g - Xpal[1]) * (g - Xpal[1]) +
  250.                     (b - Xpal[2]) * (b - Xpal[2]);
  251.  
  252.     for (i=1; i < 256; i++) {
  253.         temp_dist = (r - Xpal[(i * 3) + 0]) * (r - Xpal[(i * 3) + 0]) +
  254.                     (g - Xpal[(i * 3) + 1]) * (g - Xpal[(i * 3) + 1]) +
  255.                     (b - Xpal[(i * 3) + 2]) * (b - Xpal[(i * 3) + 2]);
  256.  
  257.         if (temp_dist < shortest_dist) {
  258.             shortest_dist = temp_dist;
  259.             shortest_pal = i;
  260.         }
  261.     }
  262.  
  263.     return i;
  264. }
  265.  
  266.